package com.huiyeji.dynamic.core.provider;


import com.huiyeji.dynamic.core.MethodBody;
import com.huiyeji.dynamic.core.MethodRequest;
import com.huiyeji.dynamic.core.MethodRequestMatchRule;
import com.huiyeji.dynamic.core.annotations.DynamicMethod;
import com.huiyeji.sort.ObjectMatcher;

import java.lang.reflect.Method;

@Deprecated
public class DynamicForceMethodProvider implements DynamicMethodProvider{

    private final ObjectMatcher<MethodRequest,MethodRequest, MethodBody> matcher = new ObjectMatcher<MethodRequest,MethodRequest, MethodBody>(new MethodRequestMatchRule());

    public DynamicForceMethodProvider(Class<?> clazz) {
        initDynamicMethods(clazz.getMethods());
    }

    @Override
    public Method get(MethodRequest request) {
        return matcher.matchObject(request).getMethod();
    }

    private void initDynamicMethods(Method[] methods) {
        for (Method method : methods) {
            String name = method.getName();
            if (method.isAnnotationPresent(DynamicMethod.class)) {
                DynamicMethod annotation = method.getAnnotation(DynamicMethod.class);
                name = "".equals(annotation.value()) ? name : annotation.value();
            }
            matcher.registerObject(new MethodBody(name, method));
        }
    }
}
